package com.example.administrator.myweiyue.ui.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.myweiyue.MyApplication;
import com.example.administrator.myweiyue.R;
import com.example.administrator.myweiyue.utils.DataCache;
import com.example.administrator.myweiyue.utils.DialogHelper;
import com.trello.rxlifecycle2.LifecycleTransformer;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by Administrator on 2018-01-23.
 */


public abstract   class MyBaseFragment<T extends BaseContract.BasePresenter> extends MySupportFragment implements BaseContract.BaseView, IBase {
    protected Unbinder unbinder;
    protected LayoutInflater layoutInflater;
    protected Activity context;
    protected View mRootView;
    protected DataCache mDataCache;


    //    protected Diycode mDiycode;
    @Nullable
    @Inject
    protected T mPresenter;
    protected AlertDialog mLoadingDialog;
//    @Nullable
//    @BindView(R.id.SimpleMultiStateView)
//    SimpleMultiStateView mSimpleMultiStateView;
    @Nullable
    @Override
    public final View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        layoutInflater = inflater;
        if (mRootView == null) {
            mRootView = inflater.inflate(getLayoutId(), null);
            unbinder = ButterKnife.bind(this, mRootView);
        } else {
            ViewGroup viewGroup = (ViewGroup) mRootView.getParent();
            if (viewGroup != null) {
                viewGroup.removeView(mRootView);
            }
        }
        mLoadingDialog = DialogHelper.getLoadingDialog(getActivity());
        return mRootView;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //dagger2绑定
        initInjector(MyApplication.getAppComponent());
        //传递view借口对象到Present中去
        attachView();
        //初始化view
        afterViewCreate(mRootView);
        //初始化各种布局
        initStateView();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initData();
    }
    private void initStateView() {
//        if (mSimpleMultiStateView == null) return;
//        mSimpleMultiStateView.setEmptyResource(R.layout.view_empty)
//                .setRetryResource(R.layout.view_retry)
//                .setLoadingResource(R.layout.view_loading)
//                .setNoNetResource(R.layout.view_nonet)
//                .build()
//                .setonReLoadlistener(new MultiStateView.onReLoadlistener() {
//                    @Override
//                    public void onReload() {
//                        onRetry();
//                    }
//                });
    }

    @Nullable
    @Override
    public View getView() {
        return mRootView;
    }

    private void attachView() {
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (useEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }

    protected boolean useEventBus() {
        return false;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (useEventBus()) {
            EventBus.getDefault().register(this);
        }

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof Activity) {
            this.context = (Activity) context;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        context = null;
    }

    public static <T extends BaseFragment> T newInstance(Class<T> clazz) {
        //Activity可传递值到Fragment   获取传来的值： String str = (String)getArguments().get(“str”);
        Bundle args = new Bundle();
        try {
            T t = clazz.newInstance();
            t.setArguments(args);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //每个子类都获取同一个缓存对象
        mDataCache = DataCache.getInstance();
        //每个子类都获取同一个请求网络管理对象
//        mDiycode = Diycode.getInstance();

    }

    protected void showLoadingDialog() {
        if (mLoadingDialog != null)
            mLoadingDialog.show();
    }

    protected void showLoadingDialog(String str) {
        if (mLoadingDialog != null) {
            TextView tv = (TextView) mLoadingDialog.findViewById(R.id.tv_load_dialog);
            tv.setText(str);
            mLoadingDialog.show();
        }
    }

    protected void hideLoadingDialog() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing())
            mLoadingDialog.cancel();
    }


    @Override
    public void showLoading() {
//        if (mSimpleMultiStateView != null) {
//            mSimpleMultiStateView.showLoadingView();
//        }
    }

    @Override
    public void showSuccess() {
//        hideLoadingDialog();
//        if (mSimpleMultiStateView != null) {
//            mSimpleMultiStateView.showContent();
//        }
    }

    @Override
    public void showFaild() {
//        if (mSimpleMultiStateView != null) {
//            mSimpleMultiStateView.showErrorView();
//        }
    }

    @Override
    public void showNoNet() {
//        if (mSimpleMultiStateView != null) {
//            mSimpleMultiStateView.showNoNetView();
//        }
    }

    @Override
    public void onRetry() {

    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.<T>bindToLifecycle();
    }

    @Override
    public void onDestroy() {
        if (unbinder != Unbinder.EMPTY) {
            unbinder.unbind();
        }
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        super.onDestroy();
    }

}
